//
//  LYStatusListViewModel.swift
//  微博
//
//  Created by liuerying on 16/5/25.
//  Copyright © 2016年 liuerying. All rights reserved.

//  负责给TableView提供模型数据


import UIKit

class LYStatusListViewModel: NSObject {

    //定义模型
    lazy var statusViewModelArray: [LYStatusViewModel] = [LYStatusViewModel]()
    
//    lazy var statues: [LYStatuses] = [LYStatuses]()
    
    func loadData(isPulling:Bool ,finished:(isSucces:Bool,count :Int)->()){
        
        var since_id: Int64 = 0
        var max_id: Int64 = 0
       
        if isPulling{
        
            max_id = statusViewModelArray.last?.status?.id ?? 0
        }else{
        
            since_id = statusViewModelArray.first?.status?.id ?? 0
        }
        
        LYNetworkTools.sharedTools.loadHomePageData(since_id, max_id: max_id) { (responseObject, error) in
  
            if error != nil{
                
                finished(isSucces: false,count: 0)
//                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
                
            }
            
            //        print(LYUserAccountModel.sharedAccountViewModel.userAccount?.access_token ?? 0)
            guard let dict = responseObject as? [String: AnyObject] else{
                finished(isSucces: false,count: 0)
                return
            }
            
            //获取statues数据
            guard let array = dict["statuses"] as? [[String : AnyObject]] else{
                finished(isSucces: false,count: 0)
                return
            }
            //            print(array)
            
            var tempArray = [LYStatusViewModel]()
            for item in array{
                
                //字典转模型
                let s = LYStatuses(dict: item)
                
                let viewmodel = LYStatusViewModel(status: s)
                
                tempArray.append(viewmodel)
            }
            
            if  isPulling{
            
                //加载更多数据
                self.statusViewModelArray = self.statusViewModelArray + tempArray
          
            }else{
            
                //刷新新数据
                self.statusViewModelArray = tempArray + self.statusViewModelArray
            }
            //保存数组
            //刷新数据
//            self.tableView.reloadData()
            
            finished(isSucces: true,count: tempArray.count)
        }
        

    
    }
    
}
